#
# Lanes/Makefile
#
#   make test
#   make tgz VERSION=x.x
#   make clean
#
# Defines:
#   CFLAGS  ...
#
MODULE = lanes

LUA_FLAGS=
LUA=

N=1000

# Autodetect LUA & LUAC
#
LUA=$(word 1,$(shell which lua5.1) $(shell which lua51) lua)
LUAC=$(word 1,$(shell which luac5.1) $(shell which luac51) luac)

PREFIX=LUA_CPATH=./src/?.so LUA_PATH=./?.lua

#---
all: src/lua51-lanes.so

src/lua51-lanes.so: src/*.lua src/*.c src/*.h src/Makefile
	cd src && make

clean:
	cd src && make clean


#--- Testing ---
#
test: tests/test.lua src/lua51-lanes.so
	$(PREFIX) $(LUA) $<

test2: tests/test2.lua src/lua51-lanes.so
	$(PREFIX) $(LUA) $<

fibonacci: tests/fibonacci.lua src/lua51-lanes.so
	$(PREFIX) $(LUA) $<

# Test cases from users:
#
stefan: tests/stefan.lua src/lua51-lanes.so
	$(PREFIX) $(LUA) $<

#---
perftest-plain: tests/perftest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< $(N) -plain"

perftest-single: tests/perftest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< $(N) -single"

perftest-prio perftest-odd: tests/perftest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< 20 -prio=+2"

perftest-even: tests/perftest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< 20 -prio=-2"

perftest: tests/perftest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< $(N)"

launchtest: tests/launchtest.lua src/lua51-lanes.so
	$(MAKE) _perftest ARGS="$< $(N)"

_perftest:
	$(PREFIX) $(TIME) $(LUA) $(ARGS)


#--- Packaging ---
#
# Make a folder of the same name as tgz, good manners (for the manual
# expander)
#
# "make tgz VERSION=yyyymmdd"
#
VERSION=

tgz:
ifeq "$(VERSION)" ""
	echo "Usage: make tar VERSION=x.x"; false
else
	$(MAKE) clean 
	-rm -rf $(MODULE)-$(VERSION)
	mkdir $(MODULE)-$(VERSION)
	tar c --exclude=.svn --exclude=.DS_Store --exclude="_*" \
	        --exclude="*.tgz" --exclude="*.rockspec" \
	        --exclude=lanes.dev --exclude="$(MODULE)-*" * \
	   | (cd $(MODULE)-$(VERSION) && tar x)
	tar czvf $(MODULE)-$(VERSION).tgz $(MODULE)-$(VERSION)
	md5sum $(MODULE)-$(VERSION).tgz
endif
	
	
#--- Undocumented ---
#
require: src/lua51-lanes.so
	$(PREFIX) $(LUA) -e "require '$(MODULE)'"

#debug:
#	make clean
#	make linux CFLAGS="-O0 -g"
#	@echo "***"
#	@echo "*** Type: run + ENTER (run the program)"
#	@echo "***       dofile 'test.lua'"
#	@echo "***"
#	gdb lua


echo:
	@echo aaa

.PROXY:	all clean test require debug _nodemo _notest

